﻿using Opc.Ua;
using Opc.Ua.Client;
using SHRIS.MIS.Common.Util.Extension;
using SHRIS.MIS.Entity.ScadaBaseInfo;
using SHRIS.MIS.Entity.ScadaData;
using SHRIS.MIS.Service.OpcService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SHRIS.MIS.Service.Device
{
    public class Scanner_13 : BasicDevice
    {
        private readonly IEnumerable<OpcDataSourceEntity> _opcDataSourceEntities;
        public Scanner_13(DeviceEntity deviceEntity,
           WorkshopCommunicationService workshopCommunicationService,
           IEnumerable<OpcDataSourceEntity> opcDataSourceEntities)
           : base(deviceEntity, workshopCommunicationService)
        {
            _opcDataSourceEntities = opcDataSourceEntities;
            CheckOutDataType();
        }

        private void CheckOutDataType()
        {
            //对所有OPC 数据类型进行校验，如果不对记入LOG

        }
        public override void InitalDevice()
        {
            //创建opc 订阅

            //找出该通道下的订阅数据，dataAction==4 为订阅
            var subscriptionDatas = _opcDataSourceEntities.Where(i => i.DataAction == 4);

            //找出订阅数据的服务器列表
            var serverIds = subscriptionDatas.Select(i => i.ServerId).Distinct().ToList();

            if (serverIds.Count() == 1)
            {
                _workshopCommunicationService.CreateOpcSubscription(subscriptionDatas, DataHandler);
            }


            //用deviceCode作为订阅组名字
            //var subscriptionName = _deviceEntity.DeviceCode;

        }

        private void DataHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
                //Console.WriteLine(notification.Value.StatusCode.ToString());

                if (notification != null)
                {
                    Guid dataId = new Guid();
                    bool isId = Guid.TryParse(monitoredItem.DisplayName, out dataId);
                    if (isId && (!dataId.IsGuidNullOrEmpty()))
                    {
                        var dataSource = _opcDataSourceEntities.Where(i => i.Id == dataId).First();
                        switch (dataSource.DataName)
                        {
                            case "StartScanCodeStatus":
                                {
                                    StartScanCodeStatus = (bool)notification.Value.Value;
                                    break;
                                }
                            case "AgainStatus":
                                {
                                    AgainStatus = (bool)notification.Value.Value;
                                    break;
                                }
                            case "PassStatus":
                                {
                                    PassStatus = (bool)notification.Value.Value;
                                    break;
                                }
                            case "ScanCodeBypassStatus":
                                {
                                    ScanCodeBypassStatus = (bool)notification.Value.Value;
                                    break;
                                }
                            case "ScanCodeCom":
                                {
                                    ScanCodeCom = (bool)notification.Value.Value;
                                    break;
                                }
                            case "ScanCodeContent":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        ScanCodeContent = notification.Value.Value.ToString();
                                    }
                                    
                                    break;
                                }
                            case "ScanCodeContent_Enter":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        ScanCodeContent_Enter = notification.Value.Value.ToString();
                                    }
                                    
                                    break;
                                }
                            case "AutoPass":
                                {
                                    AutoPass = (bool)notification.Value.Value;
                                    break;
                                }
                            case "AutoPassStatus":
                                {
                                    AutoPassStatus = (bool)notification.Value.Value;
                                    break;
                                }
                            case "AutoPassContent":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        AutoPassContent = notification.Value.Value.ToString();
                                    }
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 设备参数
        private bool startScanCodeStatus;
        public bool StartScanCodeStatus
        {
            get
            {
                return startScanCodeStatus;
            }
            set
            {
                bool previewVlaue = startScanCodeStatus;
                startScanCodeStatus = value;
                OnPropertyChanged("StartScanCodeStatus", previewVlaue, startScanCodeStatus);
            }
        }

        private bool againStatus;
        public bool AgainStatus
        {
            get
            {
                return againStatus;
            }
            set
            {
                bool previewVlaue = againStatus;
                againStatus = value;
                OnPropertyChanged("AgainStatus", previewVlaue, againStatus);
            }
        }

        private bool passStatus;
        public bool PassStatus
        {
            get
            {
                return passStatus;
            }
            set
            {
                bool previewVlaue = passStatus;
                passStatus = value;
                OnPropertyChanged("PassStatus", previewVlaue, passStatus);
            }
        }

        private bool scanCodeBypassStatus;
        public bool ScanCodeBypassStatus
        {
            get
            {
                return scanCodeBypassStatus;
            }
            set
            {
                bool previewVlaue = scanCodeBypassStatus;
                scanCodeBypassStatus = value;
                OnPropertyChanged("ScanCodeBypassStatus", previewVlaue, scanCodeBypassStatus);
            }
        }

        private bool scanCodeCom;
        public bool ScanCodeCom
        {
            get
            {
                return scanCodeCom;
            }
            set
            {
                bool previewVlaue = scanCodeCom;
                scanCodeCom = value;
                OnPropertyChanged("ScanCodeCom", previewVlaue, scanCodeCom);
            }
        }

        private string scanCodeContent;
        public string ScanCodeContent
        {
            get
            {
                return scanCodeContent;
            }
            set
            {
                string previewVlaue = scanCodeContent;
                scanCodeContent = value;
                OnPropertyChanged("ScanCodeContent", previewVlaue, scanCodeContent);
            }
        }

        private string scanCodeContent_Enter;
        public string ScanCodeContent_Enter
        {
            get
            {
                return scanCodeContent_Enter;
            }
            set
            {
                string previewVlaue = scanCodeContent_Enter;
                scanCodeContent_Enter = value;
                OnPropertyChanged("ScanCodeContent_Enter", previewVlaue, scanCodeContent_Enter);
            }
        }

        private bool autoPass;
        public bool AutoPass
        {
            get
            {
                return autoPass;
            }
            set
            {
                bool previewVlaue = autoPass;
                autoPass = value;
                OnPropertyChanged("AutoPass", previewVlaue, autoPass);
            }
        }

        private bool autoPassStatus;
        public bool AutoPassStatus
        {
            get
            {
                return autoPassStatus;
            }
            set
            {
                bool previewVlaue = autoPassStatus;
                autoPassStatus = value;
                OnPropertyChanged("AutoPassStatus", previewVlaue, autoPassStatus);
            }
        }

        private string autoPassContent;
        public string AutoPassContent
        {
            get
            {
                return autoPassContent;
            }
            set
            {
                string previewVlaue = autoPassContent;
                autoPassContent = value;
                OnPropertyChanged("AutoPassContent", previewVlaue, autoPassContent);
            }
        }
        #endregion
    }
}
